package com.ksj.task;

import com.ksj.config.WebSocketConfig;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import java.util.Random;

@Component
public class LocationSimulator {
    private final Random random = new Random();

    @Scheduled(fixedRate = 5000)
    public void sendLocationUpdates() {
        try {
            double lat = 51.505 + (random.nextDouble() - 0.5) * 0.01;
            double lng = -0.09 + (random.nextDouble() - 0.5) * 0.01;
            String location = String.format("{\"lat\": %f, \"lng\": %f}", lat, lng);
            WebSocketConfig.LocationWebSocketHandler.broadcast(location);
        } catch (Exception e) {
            System.err.println("发送位置更新失败: " + e.getMessage());
        }
    }
}